package cn.zjitc.es;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;

public class SearchIndex {

    private TransportClient client;

    @Before
    public void init() throws Exception{
        Settings settings = Settings.builder()
                .put("cluster.name","my-elasticsearch")
                .build();

        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9301))
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9302))
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9303));

    }

    private void search(QueryBuilder queryBuilder) throws Exception{
        SearchResponse searchResponse = client.prepareSearch("index_hello")
                .setTypes("article")
                .setQuery(queryBuilder)
                // 设置分页信息
                .setFrom(0)
                .setSize(5)
                .get();

        SearchHits searchHits = searchResponse.getHits();
        System.out.println("查询结果总记录数："+searchHits.getTotalHits());

        Iterator<SearchHit> iterator = searchHits.iterator();
        while (iterator.hasNext()){
            SearchHit searchHit = iterator.next();
            System.out.println(searchHit.getSourceAsString());
            System.out.println("----------文档的属性-----------");
            Map<String, Object> document = searchHit.getSource();
            System.out.println(document.get("id"));
            System.out.println(document.get("title"));
            System.out.println(document.get("content"));
        }
        client.close();
    }

    @Test
    public void testSearchById() throws Exception{
        QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1", "3");
        search(queryBuilder);
    }

    @Test
    public void testQueryByTerm() throws Exception{
        QueryBuilder queryBuilder = QueryBuilders.termQuery("title", "分隔符");
        search(queryBuilder);
    }

    @Test
    public void testQueryStringQuery() throws Exception{
        QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("这是第")
                .defaultField("title");
        search(queryBuilder, "title");
    }



    private void search(QueryBuilder queryBuilder, String highlightField) throws Exception{
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field(highlightField);
        highlightBuilder.preTags("<em>");
        highlightBuilder.preTags("</em>");
        SearchResponse searchResponse = client.prepareSearch("index_hello")
                .setTypes("article")
                .setQuery(queryBuilder)
                // 设置分页信息
                .setFrom(0)
                .setSize(5)
                .highlighter(highlightBuilder)
                .get();

        SearchHits searchHits = searchResponse.getHits();
        System.out.println("查询结果总记录数："+searchHits.getTotalHits());

        Iterator<SearchHit> iterator = searchHits.iterator();
        while (iterator.hasNext()){
            SearchHit searchHit = iterator.next();
            System.out.println(searchHit.getSourceAsString());
            System.out.println("----------文档的属性-----------");
            Map<String, Object> document = searchHit.getSource();
            System.out.println(document.get("id"));
            System.out.println(document.get("title"));
            System.out.println(document.get("content"));
            System.out.println("********高亮结果**********");
            Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
            System.out.println(highlightFields);
            HighlightField filed = highlightFields.get(highlightField);
            Text[] fragments = filed.getFragments();
            if (fragments != null){
                String title = fragments[0].toString();
                System.out.println(title);
            }

        }
        client.close();
    }
}
